package com.tedu._05springmvc2.controller;

import com.tedu._05springmvc2.mapper.UserMapper;
import com.tedu._05springmvc2.pojo.dto.UserDTO;
import com.tedu._05springmvc2.pojo.dto.UserUpdateDTO;
import com.tedu._05springmvc2.pojo.entity.User;
import com.tedu._05springmvc2.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RestfulUserController {
    private final UserMapper userMapper;

    public RestfulUserController(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

//    @RequestMapping(value="/users",method=RequestMethod.POST)
    @PostMapping("/users")
    public String addUser(UserDTO userDTO){
        System.out.println("开始注册..");
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        int num = userMapper.insert(user);
        return num>0?"注册成功":"注册失败";
    }

//    @RequestMapping(value = "/users",method = RequestMethod.PUT)
    @PutMapping("/users")
    public String updateUser(UserUpdateDTO userUpdateDTO){
        System.out.println("开始修改操作");
        User user = new User();
        BeanUtils.copyProperties(userUpdateDTO,user);
        int num = userMapper.updateById(user);
        return num>0?"修改成功":"修改失败";
    }

//    @RequestMapping(value="/users/{id}",method=RequestMethod.DELETE)
    @DeleteMapping("/users/{id}")
    public String deleteUser(@PathVariable Integer id){
        System.out.println("开始删除用户");
        System.out.println("id"+id);
        return "删除成功";
    }

//    @RequestMapping(value = "/users",method = RequestMethod.GET)
    @GetMapping("/users")
    public List<UserVO> findAll(){
        return userMapper.selectAll();
    }

}
